package gui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXLabel;

import sys.Video;
import sys.Video.Status;
import net.miginfocom.swing.MigLayout;
import java.awt.Font;
import javax.swing.ImageIcon;
import org.jdesktop.swingx.JXImagePanel;
import org.jdesktop.swingx.JXImageView;
import java.awt.Toolkit;

class VideoCellRenderer extends JPanel implements ListCellRenderer {
	
	private JLabel lbl1;
	private JLabel lbl2;
	private JProgressBar pBar;
	
	private Color background;
	private Color textColor;
	private Color selectedBackground;
	private JXImageView imagePanel;
	private JLabel lblProgress;
	
	public VideoCellRenderer() {
		setOpaque(true);
		setLayout(new MigLayout("", "[16px,left][grow,fill][right][][25%,right]", "[16px:n]"));
		
		lbl1 = new JLabel("Title");
		lbl2 = new JLabel(" Speed ");
		lbl2.setFont(new Font("Tahoma", Font.ITALIC, 11));
		pBar = new JProgressBar();
		
		background = UIManager.getColor("List.background");
		selectedBackground = UIManager.getColor("List.selectionBackground");
		textColor = UIManager.getColor("Label.foreground");
		
		imagePanel = new JXImageView();
		imagePanel.setBackground(null);
		add(imagePanel, "cell 0 0,grow");
		add(lbl1, "cell 1 0");
		add(lbl2, "cell 2 0");
		
		lblProgress = new JLabel("");
		add(lblProgress, "cell 3 0");
		add(pBar, "cell 4 0,grow");
	}
	
	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		// TODO handle selection events
		
		if(isSelected) {
			this.setBackground(selectedBackground);
			lbl1.setForeground(background);
			lbl2.setForeground(background);
			lblProgress.setForeground(background);
		} else {
			this.setBackground(background);
			lbl1.setForeground(textColor);
			lbl2.setForeground(textColor);
			lblProgress.setForeground(textColor);
		}
		
		if(((Video)value).getStatus().equals(Status.WAITING)) {
			imagePanel.setImage(Toolkit.getDefaultToolkit().getImage(VideoCellRenderer.class.getResource("/res/package-supported.png")));
		}
		if(((Video)value).getStatus().equals(Status.DOWNLOADING)) {
			imagePanel.setImage(Toolkit.getDefaultToolkit().getImage(VideoCellRenderer.class.getResource("/res/package-downgrade.png")));
		}
		if(((Video)value).getStatus().equals(Status.DONE)) {
			imagePanel.setImage(Toolkit.getDefaultToolkit().getImage(VideoCellRenderer.class.getResource("/res/package-install.png")));
		}

		lblProgress.setText(((Video)value).getDownloadProgress()+"%");
		pBar.setValue(((Video)value).getDownloadProgress());
		lbl1.setText(((Video)value).getTitle());
		lbl2.setText(((Video)value).getStatusAsString());
		
		
		return this;
	}
	
}